package com.aceleeyy.apb.utils;

import cn.hutool.core.util.StrUtil;
import com.aceleeyy.apb.constants.FilterMatches;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 业务工具类
 *
 * @author Ace Lee
 * @date 2018/11/6 10:41
 **/
public class ApbUtils {

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 驼峰转下划线
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 解析过滤条件
     *
     * @param filter
     * @return
     */
    public static String parseFilters(String filter) {
        if (StrUtil.isBlank(filter)){
            return filter;
        }
        filter+=" ";//防止'出现在末尾
        String[] keys = filter.split("'");
        if (keys.length==0){
            return filter;
        }
        StringBuilder sb = new StringBuilder();
        String s = null;
        for (int i = 0; i < keys.length; i++) {
            if (i%2==0){
                s = humpToLine(keys[i]);
                s = FilterMatches.parseFilter(s);
            }else {
                s = keys[i];
            }
            sb.append("'");
            sb.append(s);
        }

        return parseLikeFilter(sb.toString().substring(1));
    }

    public static String parseLikeFilter(String filter) {
        String[] keys = filter.split("'");
        if (keys.length==0){
            return filter;
        }
        StringBuilder sb = new StringBuilder();
        String s = null;
        List<Integer> starswith = Lists.newArrayList();
        List<Integer> endswith = Lists.newArrayList();
        List<Integer> contains = Lists.newArrayList();
        for (int i = 0; i < keys.length; i++) {
            s = keys[i];
            if (i%2==0){
                if (FilterMatches.containsStarswith(s)){
                    starswith.add(i);
                }
                if (FilterMatches.containsEndswith(s)){
                    endswith.add(i);
                }
                if (FilterMatches.containsContains(s)){
                    contains.add(i);
                }
            }
        }
        if (starswith.size()>0 || endswith.size()>0 || contains.size()>0){
            int starswithTarget = -1;
            int endswithTarget = -1;
            int containsTarget = -1;
            for (int j = 0; j < keys.length; j++) {
                s = keys[j];
                if (starswith.contains(j)){
                    starswithTarget = j+1;
                    s = FilterMatches.parseStarswith(s);
                }
                if (starswithTarget==j){
                    s = keys[j]+"%";
                }
                if (endswith.contains(j)){
                    endswithTarget = j+1;
                    s = FilterMatches.parseEndswith(s);
                }
                if (endswithTarget==j){
                    s = "%"+keys[j];
                }
                if (contains.contains(j)){
                    containsTarget = j+1;
                    s = FilterMatches.parseContains(s);
                }
                if (containsTarget==j){
                    s = "%"+keys[j]+"%";
                }
                sb.append("'");
                sb.append(s);
            }
            return sb.toString().substring(1);
        }else {
            return filter;
        }
    }

}
